import koa from 'koa';
import koaStatic from 'koa-static';
import koaMount from 'koa-mount'
import api from './api/index'

const app = new koa();

//灰度和研发环境处理
app.use(async (ctx, next) => {
  if (ctx.query.grayscale) {
    // cookies设置灰度标识grayscale, ng配置据此判断走灰度还是走生产
    ctx.cookies.set('grayscale', ctx.query.grayscale, {
      httpOnly: false,
      expires: new Date(new Date().getTime() + 2 * 60 * 60 * 1000)
    });
  }
  await next();
});


// app.use(async (ctx,next)=>{
//   console.log(process.env.NODE_ENV,ctx.request.path);
//   await next();
// })

app.use(koaMount('/website/auxiliaryTool/static', koaStatic('./static/')));
app.use(koaMount('/website/auxiliaryTool/page', koaStatic('./page/')));
app.use(api);

let port = process.env.PORT;
if (!port) {
  port = 3001;
}

console.log('env:' + process.env.NODE_ENV);

app.listen(port, () => console.log(`启动Server started on port ${port}`))
